package com.paic.mhis.hcpms.audit.biz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.core.auth.biz.service.impl.FwaUserDetials;
import com.paic.mhis.core.user.biz.service.UserInfoService;
import com.paic.mhis.core.user.dto.UserInfoDTO;
import com.paic.mhis.hcpms.audit.biz.service.AuditQuartzService;
import com.paic.mhis.hcpms.audit.biz.service.AuditWorkFlowService;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dao.AuditQuartzDao;
import com.paic.mhis.hcpms.audit.dto.AuditCaseDTO;
import com.paic.mhis.hcpms.feedback.intergration.dao.AuditFeedbackDAO;
import com.paic.mhis.hcpms.warning.biz.service.WarningFromAuditService;
import com.paic.pafa.app.biz.service.BusinessServiceException;

/**
 * 
 * @author EX-CAOTIANHUA001
 * 
 */
@Service("auditQuartzServiceImpl")
public class AuditQuartzServiceImpl implements AuditQuartzService {

	/**
	 * 协议信息注入
	 */
	private static final Logger logger = Logger
			.getLogger(AuditQuartzServiceImpl.class);

	@Autowired
	private AuditWorkFlowService auditWorkFlowService; // 流程服务

	@Autowired
	private AuditQuartzDao auditQuartzDao;

	@Autowired
	private AuditFeedbackDAO auditFeedbackDAO;

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private TransactionTemplate transactionTemplate;

	@Autowired
	private WarningFromAuditService warningFromAuditService;

	@Override
	public void insureOrgQuartz() {
		warningFromAuditService.notice();
		AuditCaseDTO dto = new AuditCaseDTO();
		dto.setCaseState(AuditConstants.auditCaseStatus_5);
		dto.setCaseQuarzDay(5);
		UserInfoDTO userDto = userInfoService
				.queryByUserName(AuditConstants.AUDIT_AUTO_JOB_USER);
		UserDetails user = new FwaUserDetials(userDto);
		List<AuditCaseDTO> rlist = auditQuartzDao.queryCase4Quartz(dto);
		for (AuditCaseDTO auditCaseDTO : rlist) {
			try {
				logger.info("----auditQuartzServiceImpl.insureOrgQuartz----------caseCode:"
						+ dto.getCaseCode() + "-----------------------------");
				eachOrgInsure(auditCaseDTO, user);
			} catch (BusinessServiceException e) {
				e.printStackTrace();
				logger.error(e);
			}
		}
	}

	/**
	 * 处理确认事件
	 * 
	 * @param dto
	 * @param user
	 * @throws BusinessServiceException
	 */
	private synchronized void eachOrgInsure(final AuditCaseDTO dto,
			final UserDetails user) throws BusinessServiceException {
		transactionTemplate.execute(new TransactionCallback<Object>() {
			@Override
			public Object doInTransaction(TransactionStatus ts) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("caseCode", dto.getCaseCode());
				map.put("caseState", AuditConstants.auditCaseStatus_6);
				auditFeedbackDAO.updateAuditCase(map);
				Map<String, String> condition = new HashMap<String, String>();
				condition.put(AuditConstants.AUDIT_CASE_AFFIRM_TYPE,
						AuditConstants.AUDIT_CASE_AFFIRM_TYPE_01);
				try {
					auditWorkFlowService.processWorkFlow(dto.getCaseCode(),
							user, condition, AuditConstants.TASK_TYPE_CASE);
				} catch (Exception e) {
					logger.info("----caseCode:" + dto.getCaseCode()
							+ "-----确认出错----------");
					e.printStackTrace();
					ts.setRollbackOnly();
				}
				return null;
			}
		});
	}

}
